<template>
  <div>
    <el-form ref="deptForm" :model="form" :rules="rulesForm" label-width="120px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
          <el-option v-for="item in principalList" :key="item.id" :value="item.username" :label="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
        <el-button size="small" @click="hCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getSimple } from '@/api/employees'
import { addDepartmentsInfo, updateDepartmentsInfo } from '@/api/departments'

export default {
  name: 'VueAdminTemplateMasterAdddialog',
  props: {
    dialogTableVisible: {
      type: Boolean,
      default: false
    },
    pid: {
      type: String,
      default: ''
    },
    isEditor: {
      type: Boolean,
      default: false
    },
    row: {
      type: Object,
      default() {
        return {}
      }
    }
  },

  data() {
    return {
      form: {
        name: '',
        code: '',
        manager: '',
        introduce: '',
        pid: ''
      },
      rulesForm: {
        name: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '请输入活动名称', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ]
      },
      principalList: []
    }
  },

  watch: {
    isEditor: {
      handler(newVal) {
        if (newVal) {
          this.form = { ...this.row }
        }
      },
      // 首次加载也监听
      immediate: true
    }
  },

  created() {
    this.getSimple()
  },

  methods: {
    // 获取员工简单列表
    async getSimple() {
      const res = await getSimple()
      if (res.code === 10000) {
        this.principalList = res.data
      }
    },
    // 取消
    hCancel() {
      this.$emit('update:dialogTableVisible', false)
    },
    // 确定
    hSubmit() {
      this.$refs['deptForm'].validate(async valid => {
        if (valid) {
          this.form.pid = this.pid
          const res = await (this.isEditor ? updateDepartmentsInfo(this.form) : addDepartmentsInfo(this.form))
          if (res.code === 10000) {
            this.$emit('update:dialogTableVisible', false)
            this.$emit('success')
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
